from django.contrib.auth import authenticate
from django.shortcuts import render
from rest_framework import generics, mixins
from rest_framework.response import Response
from rest_framework import permissions

from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework.permissions import AllowAny

from match.pagination import MyPageNumberPagination
from .models import UserProfile
from .serializers import MyTokenObtainPairSerializer, UserProfileSerializer, UserProfileDetalSerializer


class MyObtainTokenPairView(TokenObtainPairView):
    """
    post:
    登陆，
    return: 123321123
    """
    permission_classes = (AllowAny,)  # 每个人都可以登陆
    serializer_class = MyTokenObtainPairSerializer


class AuthTestView(generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request):
        return Response({
            'code': '200',
            'msg': '认证成功'
        })

    pass


class UserVlistView(mixins.CreateModelMixin,
                    generics.GenericAPIView):
    pagination_class = MyPageNumberPagination
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UserProfile.objects.all()

    def post(self, request, *args, **kwargs):
        """
        注册
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        pass


class PasswordVlistView(mixins.CreateModelMixin, generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = UserProfileDetalSerializer


    def put(self, request):
        data = request.data
        username = data.get('username')
        old_password = data.get('old_password')
        new_password = data.get('new_password')
        if (not all([username, old_password, new_password])):
            return Response({
                "msg": '参数不全'
            })
        user = authenticate(username=username, password=old_password)
        if (not user):
            return Response({
                'msg': '密码错误'
            })
        if (user != request.user):
            return Response({
                'msg': '你只能修改你自己的密码'
            })
        user.set_password(new_password)
        user.save()
        serializer = self.get_serializer(user.profile)
        return Response(serializer.data)


class UsersProfileList(mixins.ListModelMixin,
                       mixins.CreateModelMixin,
                       generics.GenericAPIView):
    pagination_class = MyPageNumberPagination
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileDetalSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):  # post方法接受用户发过来的请求数据后，有时只需转发不需要创建模式对象实例，所以post方法不能简单等于create方法
        return self.create(request, *args, **kwargs)


class UsersProfileDetail(mixins.RetrieveModelMixin,
                         mixins.UpdateModelMixin,
                         mixins.DestroyModelMixin,
                         generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
